package com.ljh.lottery.controller;

import com.ljh.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.ljh.lottery.common.exception.ControllerException;
import com.ljh.lottery.common.utils.BeanTransUtil;
import com.ljh.lottery.common.utils.JacksonUtil;
import com.ljh.lottery.pojo.request.ShortMessageLoginRequest;
import com.ljh.lottery.pojo.request.UserPasswordLoginRequest;
import com.ljh.lottery.pojo.request.UserRegisterRequest;
import com.ljh.lottery.pojo.response.BaseUserInfoResponse;
import com.ljh.lottery.pojo.response.CommonResult;
import com.ljh.lottery.pojo.response.UserLoginResponse;
import com.ljh.lottery.pojo.response.UserRegisterResponse;
import com.ljh.lottery.service.UserService;
import com.ljh.lottery.service.VerificationCodeService;
import com.ljh.lottery.service.dto.UserDTO;
import com.ljh.lottery.service.dto.UserLoginDTO;
import com.ljh.lottery.service.dto.UserRegisterDTO;
import com.ljh.lottery.service.enums.UserIdentityEnum;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Themberfue
 * @date: 2025/4/8 12:30
 * @description:
 */
@RequestMapping("/user")
@RestController
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Resource(name = "userServiceImpl")
    UserService userService;

    @Resource(name = "verificationCodeServiceImpl")
    private VerificationCodeService verificationCodeService;

    /**
     * 注册
     * @param param 注册参数
     * @return 注册响应
     */
    @PostMapping("/register")
    public CommonResult<UserRegisterResponse> register(@RequestBody @Validated UserRegisterRequest param) {
        // TODO 打印日志
        log.info("userRegister UserRegisterRequest: {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(
                BeanTransUtil.transToUserRegisterResponse(
                        userService.register(param)));
    }

    /**
     * 通过密码登录
     * @param param 登录参数
     * @return 登录响应
     */
    @PostMapping("/loginByPassword")
    public CommonResult<UserLoginResponse> loginByPassword(@RequestBody @Validated UserPasswordLoginRequest param) {
        // TODO 打印日志
        log.info("loginByPassword UserPasswordLoginRequest: {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(
                BeanTransUtil.transToUserLoginResponse(
                        userService.login(param)));
    }

    /**
     * 通过短信验证码登录
     * @param param 登录参数
     * @return 登录响应
     */
    @PostMapping("/loginByShortMessage")
    public CommonResult<UserLoginResponse> loginByShortMessage(@RequestBody @Validated ShortMessageLoginRequest param) {
        // TODO 打印日志
        log.info("loginByShortMessage ShortMessageLoginRequest: {}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(
                BeanTransUtil.transToUserLoginResponse(
                        userService.login(param)));
    }

    /**
     * 发送短信验证码
     * @param phoneNumber 手机号
     * @return 发送是否成功
     */
    @PostMapping("/sendVerificationCode")
    public CommonResult<Boolean> sendVerificationCode(@NotBlank String phoneNumber) {
        // TODO 1. 打印日志
        log.info("sendVerificationCode phoneNumber: {}", phoneNumber);

        // TODO 2. 发送验证码
        verificationCodeService.sendVerificationCode(phoneNumber);

        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 根据 identity 获取用户信息
     * @param identity 身份信息
     * @return 用户信息列表
     */
    @GetMapping("/getAllUserInfo")
    public CommonResult<List<BaseUserInfoResponse>> getAllUserInfo(String identity) {
        // TODO 打印日志
        log.info("getUserInfo identity: {}", identity);

        return CommonResult.success(
                userService.getAllUserInfo(
                        UserIdentityEnum.forName(identity))
                        .stream()
                        .map(BeanTransUtil::transToBaseUserInfoResponse)
                        .toList());
    }
}
